<form action="index.php/payment_plan/insert/" method="post" name="myform" id="myform">
<div id="inline5">
<h2><span>详细信息</span></h2>
<table>
        <tr><th colspan="5">回款计划：</th></tr>
        <tr>
            <td class="name" width="100"><span>*</span>客户：</td>
            <td colspan="4"><input id="customer_name" autocomplete="off" style="" type="text" name="customer_name" value=></input></td>
        </tr>
        <tr>
        	<td class="name" width="100">合同/订单：</td>
            <td colspan="4">
				<select id="plan_contacter_name" name="contract_id" style=" width:275px;">
                </select>
            </td>
        </tr>
        <tr>
            <td class="name" width="100">计划回款日期：</td>
            <td><input name="payment_time" type="text" class="datepicker" id="find_time"  style="background:url(images/calendar.gif) right 1px no-repeat;" value=<?= $update_sales->find_time?> ></input></td>
        </tr>
        <tr class="je_g">
            <td class="name" width="100">期次：</td>
            <td>
            	<select name="stages" id="stages">
                	 <?php for($i=1;$i<=12;$i++){?>
                	<option value="<?= $i?>"><?php echo $i;?></option>
                    <?php }?>
                </select>
            </td>
        </tr>
        <script type="text/javascript">
		$(document).ready(function(){
			$("#stages").change(function(){
				var je='';
				for(var i=0;i<$("#stages").val();i++){
					je+='<tr class="je_g_c"><td class="name" width="100">金额：</td> <td><input class="input_bg1" name="money'+i+'" id="money'+i+'" type="text" id="principal" value= ></input><input type="text" name="stages_payment_time'+i+'" class="input_bg2 datepicker" /></td></tr>';	
				}
				$(".je_g_c").html("");	
				$(".je_g").after(je);
				$( ".datepicker" ).datepicker();
			})	
		});	
		</script>
        <tr class="je_g_c">
            <td class="name" width="100">金额：</td>
            <td><input name="money" type="text" class="input_bg1" id="principal" value= ></input>
            <input type="text" name="stages_payment_time" class="input_bg2 datepicker" /></input></td>
        </tr>
        <tr>
            <td class="name" width="100">所有者：</td>
            <td>
            	<select name="owner_id" style=" width:275px;">
            		<?php foreach($users as $user):?>
            			<?php if($user->user_name != '何俊斌'):?>
            				<option value="<?= $user->id?>" <?php if($this->session->userdata('username') == $user->user_name):?>selected="selected"<?php endif;?>><?= $user->user_name?></option>
            			<?php endif;?>
                	<?php endforeach;?>
                </select>
            </td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr class="sub">
            <td colspan="5"><input type="button" id="tijiao" value="提交" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="重置" /></td>
        </tr>
    </table>
</div>
</form>
<input type="hidden" id="type" value="2" ></input>
<input type="hidden" id="type_record" value="" ></input>
<script type="text/javascript">
$(function() {
		$( ".datepicker" ).datepicker();

		$("#tijiao").click(function(){
			if(!$("#customer_name").val()){
				alert('请填写客户');
				$("#customer_name").focus();
			}else{
				var name =$("#plan_contacter_name").val();
				$.post("index.php/payment_plan/check_contract", { contract_id: name},
						   function(data){
						    if(data > 0){
							    alert('合同已经创建回款计划');
							}else{
								var contract_id = $("#plan_contacter_name").val();
								$.post("index.php/payment_plan/get_contract_money", { contract_id: contract_id},
										function(data){
											var val = $("#stages").val();
											var total=0;
											if($("#principal").val()){
												    total = parseFloat($("#principal").val());
											}else{
												for(var i=0;i<val;i++){
													var money=$("#money"+i+"").val();
													total+=parseFloat(money);
												}
											}
											var total_money = data;
											if(total > total_money){
												alert('输入的金额大于合同总金额');
											}
											if(total < total_money){
												alert('输入的金额小于合同总金额');
											}
											if(total == total_money){
												$("#myform").submit();
											}
									 });
								
							}
				});
			}
		})
});
</script>